package com.ssmk.nfc;

import android.nfc.Tag;
import android.nfc.tech.NfcA;

import java.io.IOException;
import java.util.Arrays;


public class Ntag21X {
	
	private Tag ntag;
	private NfcA nfcA;
	private int NtagPages = 0; 
	private byte[] ReadError = {(byte)0xee};
	
	public Ntag21X(Tag ntag) {
		super();
		this.ntag = ntag;
        this.nfcA = NfcA.get(this.ntag);
        this.nfcA.setTimeout(500);
	}
	
	public void ntagConnect()
	{
        try {
			nfcA.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
		}    
	}
	
	public void ntagClose() 
	{
		if(nfcA.isConnected())
		{
			try {
				nfcA.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public byte[]  ntagGetVersion()
	{	
		try {
			byte[] Cmd = { (byte) 0x60};
			byte[] result = nfcA.transceive(Cmd);
			return result;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ReadError;
		}
	}
	
	public String ntagICInformation(byte [] version_result)
	{
		String IC = "UNKNOW_IC";
		if(version_result[1]==0x04)
		{
			 IC = "NTAG_IC";
			 if(version_result[6]==0x0f)
			 {
				 IC = "NTAG_213";
				 this.NtagPages = 45;
			 }
			 if(version_result[6]==0x11)
			 {
				 IC = "NTAG_215";
				 this.NtagPages = 135;
			 }
			 if(version_result[6]==0x13)
			 {
				 IC = "NTAG_216";
				 this.NtagPages = 261;
			 }			
		}
		return IC;					
	}
	
	public byte[] readFourPages(byte addr)
	{	
        try
        {
    		byte[] cmd = { (byte) 0x30,addr};
            byte[] result = nfcA.transceive(cmd);
            return result;
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return ReadError;
	}
	
	
	}

	public byte[] readSomePages(byte Saddr,byte Eaddr)
	{
        try
        {
    		byte[] cmd = { (byte) 0x3A,Saddr,Eaddr};
            byte[] result = nfcA.transceive(cmd);
            return result;
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return ReadError;
	}
	}

	public int getMemoryPages()
	{
		return this.NtagPages;
	}

	public byte[] writePage(byte addr, byte[] data)
	{
        try
        {
    		byte[] cmd = { (byte) 0xA2,addr,data[0],data[1],data[2],data[3]};
            byte[] result = nfcA.transceive(cmd);
            return result;
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return ReadError;
        }
	}
	
	public String hex2Str(byte[] data)
	{
		String results = "";
		 String hex="";
         for(int i=0;i<data.length;i++)
         {
             hex = Integer.toHexString(data[i] & 0xFF);
             if (hex.length() == 1)
             { 
                 hex='0' + hex;
             }
             results = results+hex;
         }
         return results;
	}
	
	public String ntagAuth(byte[] pwd, byte[] pack)
	{
        try
        {
    		byte[] cmd = { (byte) 0x1B,pwd[0],pwd[1],pwd[2],pwd[3]};
            byte[] result = nfcA.transceive(cmd);
            if(Arrays.equals(pack,result))
            {
            	return "Auth_Success";
            }
            else
            {
            	return "Auth_fail";
            }    	
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return "Auth_fail";
        }
	}
	//缺少Check NAK函数
}


